package com.yongchun.controller;

import com.alibaba.fastjson.JSONObject;
import com.yongchun.commons.NumberUtil;
import com.yongchun.commons.StringUtil;
import com.yongchun.commons.YXDate;
import com.yongchun.config.suningpay.FtpgsUtils;
import com.yongchun.model.Player;
import com.yongchun.pubInter.SkException;
import com.yongchun.pubInter.SkJsonResult;
import com.yongchun.service.IPlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author afeng
 * @version 1.0
 * @date 2023/11/15 下午8:06
 */
@RequestMapping("/snPay/")
@Controller
public class SunNingPayController {
    @Value("${suning-pay-config.mchtNo}")
    private String suningPayMchtNo;
    @Value("${suning-pay-config.appId}")
    private String suningPayAppId;
    @Value("${suning-pay-config.privateKey}")
    private String suningPayPrivateKey;





//    privateKey: MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCP0JotNqKDzBs++pyTmVH5P8DhXN7AIk/6miY4Sdh6UxMVUfoXRFpgtmoZ1WZCQJkxlYXQDSpEAnzL7ixKLhlnVzUbl6ICdpydVrHohmvyoJrHm5Va7y7tZVWslqgF8vymbDIW/8zie/PjjM4UqJm++xXJfCJXiudiQnYBLPB8YOKCRGl7otamw+j2epDZKJ3BKBFPvvt6kUlV1myS2skD78TbQ/xuSgQ9G+Xe1Nkex0fFEcIqVF9XImuvsH9Z6wfwZ+enBKVqZDrvm9B1xsuXhByAi0QtCJb5OlfD3zpVCIFWK1aNN+IVJZzMFHkdd1YNRncebkukR0WXsZzvKRZbAgMBAAECggEAH6gRaAZwu4ZAMRrFQJAtRSpNcM4akjx7UnSAGT/YEQUcXEMRQ/Hd2xAMRud8fPTvQ8TgEa30MchCZDlnMmT3XpIJtKsqFFS7oL6mpsKAHIZRy8Jpm/yuXb9Cnp0DG1w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DHIuphY=
//    publicKey: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAj9CaLTaig8wbPvqck5lR+T/A4VzewCJP+pomOEnYelMTFVH6F0RaYLZqGdVmQkCZMZWF0A0qRAJ8y+4sSi4ZZ1c1G5eiAnacnVax6IZr8qCax5uVWu8u7WVVrJaoBfL8pmwyFv/M4nvz44zOFKiZvvsVyXwiV4rnYkJ2ASzwfGDigkRpe6LWpsPo9nqQ2SidwSgRT777epFJVdZsktrJA+/E20P8bkoEPRvl3tTZHsdHxRHCKlRfVyJrr7B/WesH8GfnpwSlamQ675vQdcbLl4QcgItELQiW+TpXw986VQiBVitWjTfiFSWczBR5HXdWDUZ3Hm5LpEdFl7Gc7ykWWwIDAQAB
//    url: https://mfg.suning.com/ftpgs/trade/aggrpay/aggrProgramPay
//    signType: RSA
//    noticeUrl: https://payseryc.mj.9lhgame.com/snPay/callback
//    returnUrl: https://payseryc.mj.9lhgame.com/snPay/success



    @Autowired
    private IPlayerService playerService;

    @GetMapping("success")
    public String success() {
        return "success";
    }

    //    苏宁支付
    @GetMapping("programPay")
    public String programPay(
            @RequestParam(required = false) String pid,
            @RequestParam(required = false) String t,
            @RequestParam(required = false) Float m,
            Model model
    ) throws IOException, SkException {

        Player player = playerService.getDetail(pid);

        String payMoney = String.format("%.2f", NumberUtil.divideBigDecimal(new BigDecimal(m), new BigDecimal(100)));
        Map<String, Object> params = new HashMap<>();
        params.put("mchtNo", "SJ102524");
        params.put("orderName", "商品购买");
        params.put("amount", payMoney);
        String orderId = "SN1___" + StringUtil.createOrderNo(6) + System.currentTimeMillis();
        System.out.println(orderId);
        params.put("orderId", orderId);
        params.put("noticeUrl", "https://payseryc.mj.9lhgame.com/adapay/callback");
        params.put("returnUrl", "http://payseryc.mj.9lhgame.com/");
        params.put("remark", "suningpaysuccess");


        FtpgsUtils.AuthParam authParam = new FtpgsUtils.AuthParam();
        authParam.setAppId("yfbmSJ102524e2022110801");
//        新
        authParam.setPr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TAAQomz+/qcwt1U8bOMcFiEYianxcwKBgGRFT4uZ4gNHqwvhHggFS8gtuPjuNC3Qnvh4ZFH9y5vT+htKu5TDpWA749gDX3s40obpJpLSfODd2mS49KyfryJF5UZaLvXqviJeW3aEAGkurmH4K/yVT7rMD+F4VvNe3zqH5l2PaLRiaU5MtgqGxk/uyVAViR6DAnUpDBanEAv5AoGASJABXD3EhqDcMesSZ3fiT7qvTnUh6ohMrb5GYIY3kXWab4y0peMVkjol5zK/iW6JcYzyGYozFo3xOa5JDXYGgPTtrTlDeZXuUWBVE4uXOSyGf5IH2c12cEQbvxWIlDW4dID3hfrn0/lFu9hHF24LDsOzd/Eaf1r8iCEAyLvQkF0CgYEAy4d830asTVwaOEHKuz9sp9vbeYD6Ov7BN68yUIDTSB51zGsBhP0LiyOwPtaRmEsbGRzokJuBnmjzZGYEUr/2TG5W29ihiRZ6n4WqK8ELQf3ZKnBXoKc+naBmhPZO1xeyEydFWhf8LaSI/xlth0+dGaMQyAiImaF/BYavDHIuphY=");
        authParam.setPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAj9CaLTaig8wbPvqck5lR+T/A4VzewCJP+pomOEnYelMTFVH6F0RaYLZqGdVmQkCZMZWF0A0qRAJ8y+4sSi4ZZ1c1G5eiAnacnVax6IZr8qCax5uVWu8u7WVVrJaoBfL8pmwyFv/M4nvz44zOFKiZvvsVyXwiV4rnYkJ2ASzwfGDigkRpe6LWpsPo9nqQ2SidwSgRT777epFJVdZsktrJA+/E20P8bkoEPRvl3tTZHsdHxRHCKlRfVyJrr7B/WesH8GfnpwSlamQ675vQdcbLl4QcgItELQiW+TpXw986VQiBVitWjTfiFSWczBR5HXdWDUZ3Hm5LpEdFl7Gc7ykWWwIDAQAB");

        authParam.setVersion("1.0");
        authParam.setSignKeyIndex("0001");
        authParam.setSignType("RSA");
        authParam.setTimestamp(YXDate.getFormatDate2String(new Date(), "yyyyMMddHHmmss"));
        authParam.setUrl("https://mfg.suning.com/ftpgs/trade/aggrpay/aggrProgramPay");
        authParam.setParams(params);
        //1. 新版鉴权方式 获取鉴权参数的JSON格式字符串
        String authParamString = FtpgsUtils.makeNewAuthParam(authParam);
        //2.发送HTTP网络请求，得到响应结果(此处可自行替换成自己的HTTP工具类，如httpClinet)
        String res = FtpgsUtils.post(authParam.getUrl(), authParamString, 5000, 5000);
        System.out.println("http响应结果为：" + res);
        JSONObject backData = JSONObject.parseObject(res);
        JSONObject paramObj = backData.getJSONObject("params");

        model.addAttribute("orderId", paramObj.getString("tradeOrderId"));

        //3.验签返回结果签名是否一致(如果不需要对返回结果验签，可跳过此步骤)
        boolean verifySign = FtpgsUtils.verifySign(res, authParam.getPublicKey());
        System.out.println("验证结果为：" + verifySign);

        model.addAttribute("player", player);
        return "suNinPay";
    }

    @PostMapping("onlyPay")
    public SkJsonResult onlyPay() throws IOException {
        String openId = "ogq-K6Kio8R-0XLuSz0JgL8yX7-4";//湖南祺穗网络科技有限公司
        String wxAppId = "wxc5f1e40c659fd801";
        Map<String, Object> params = new HashMap<>();
        params.put("mchtNo", "SJ102524");
        String orderId = "SN1___" + StringUtil.createOrderNo(6) + System.currentTimeMillis();
        System.out.println(orderId);
        params.put("orderId", orderId);
        params.put("amount", "0.01");
        params.put("userId", openId);
        params.put("payType", "WX");
        params.put("mAppid", wxAppId);

        params.put("noticeUrl", "https://payser.mj.9lhgame.com/adapay/callback");
        params.put("orderName", "订单支付了哦");
        params.put("remark", "suningpaysuccess");


        FtpgsUtils.AuthParam authParam = new FtpgsUtils.AuthParam();
        authParam.setAppId("yfbmSJ102524e2022110801");
        authParam.setPrivateKey("MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCP0JotNqKDzBs++pyTmVH5P8DhXN7AIk/6miY4Sdh6UxMVUfoXRFpgtmoZ1WZCQJkxlYXQDSpEAnzL7ixKLhlnVzUbl6ICdpydVrHohmvyoJrHm5Va7y7tZVWslqgF8vymbDIW/8zie/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BanEAv5AoGASJABXD3EhqDcMesSZ3fiT7qvTnUh6ohMrb5GYIY3kXWab4y0peMVkjol5zK/iW6JcYzyGYozFo3xOa5JDXYGgPTtrTlDeZXuUWBVE4uXOSyGf5IH2c12cEQbvxWIlDW4dID3hfrn0/lFu9hHF24LDsOzd/Eaf1r8iCEAyLvQkF0CgYEAy4d830asTVwaOEHKuz9sp9vbeYD6Ov7BN68yUIDTSB51zGsBhP0LiyOwPtaRmEsbGRzokJuBnmjzZGYEUr/2TG5W29ihiRZ6n4WqK8ELQf3ZKnBXoKc+naBmhPZO1xeyEydFWhf8LaSI/xlth0+dGaMQyAiImaF/BYavDHIuphY=");
        authParam.setPublicKey("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAj9CaLTaig8wbPvqck5lR+T/A4VzewCJP+pomOEnYelMTFVH6F0RaYLZqGdVmQkCZMZWF0A0qRAJ8y+4sSi4ZZ1c1G5eiAnacnVax6IZr8qCax5uVWu8u7WVVrJaoBfL8pmwyFv/M4nvz44zOFKiZvvsVyXwiV4rnYkJ2ASzwfGDigkRpe6LWpsPo9nqQ2SidwSgRT777epFJVdZsktrJA+/E20P8bkoEPRvl3tTZHsdHxRHCKlRfVyJrr7B/WesH8GfnpwSlamQ675vQdcbLl4QcgItELQiW+TpXw986VQiBVitWjTfiFSWczBR5HXdWDUZ3Hm5LpEdFl7Gc7ykWWwIDAQAB");
        authParam.setVersion("1.0");
        authParam.setSignKeyIndex("0001");
        authParam.setSignType("RSA");
        authParam.setTimestamp(YXDate.getFormatDate2String(new Date(), "yyyyMMddHHmmss"));
        authParam.setUrl("https://mfg.suning.com/ftpgs/trade/aggrpay/aggrOnlyPay");
        authParam.setParams(params);
        //1. 新版鉴权方式 获取鉴权参数的JSON格式字符串
        String authParamString = FtpgsUtils.makeNewAuthParam(authParam);
        System.out.println("http请求参数为：" +authParamString);
        //2.发送HTTP网络请求，得到响应结果(此处可自行替换成自己的HTTP工具类，如httpClinet)
        String res = FtpgsUtils.post(authParam.getUrl(), authParamString, 5000, 5000);
        System.out.println("http响应结果为：" + res);
        //3.验签返回结果签名是否一致(如果不需要对返回结果验签，可跳过此步骤)
//        boolean verifySign = FtpgsUtils.verifySign(res, authParam.getPublicKey());
//        System.out.println("验证结果为：" + verifySign);

        JSONObject backData = JSONObject.parseObject(res);
        JSONObject paramObj = backData.getJSONObject("params");
        Map<String, Object> backObj=new HashMap<>();
        backObj.put("appId",wxAppId);
        backObj.put("timeStamp",paramObj.getLong("timeStamp"));
        backObj.put("nonceStr",paramObj.getString("noncestr"));
        backObj.put("package",paramObj.getString("packageInfo"));
        backObj.put("signType",paramObj.getString("signType"));
        backObj.put("paySign",paramObj.getString("paySign"));


        return SkJsonResult.ok().setData(backObj);
    }



}
